package com.jzo2o.orders.dispatch.test.strategys;

import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.orders.dispatch.model.dto.ServeProviderDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 策略抽象类
 *
 * @Author mr.wu
 * @Date 2024-12-29 11:17
 */
@Slf4j
public abstract class AbstractStrategyImpl implements IProcessStrategy {

    private final IProcessRule processRule;

    public AbstractStrategyImpl(){
        this.processRule = getRules();
    }


    protected  abstract IProcessRule getRules();


    /**
     * 根据指定策略获取唯一的服务人员
     *
     * @param serveProviderDTOList
     * @return
     */
    @Override
    public ServeProviderDTO getPrecedenceProvicder(List<ServeProviderDTO> serveProviderDTOList) {
        List<ServeProviderDTO> dtoList = processRule.filter(serveProviderDTOList);
        int size = CollUtils.size(dtoList);
        if(size==0) {
            log.error("没有匹配到服务人员" );
            return null;
        }
        if(size==1) {
            ServeProviderDTO providerDTO = dtoList.get(0);
            log.info("匹配到唯一的服务人员：{}" , providerDTO);
            return providerDTO;
        }
        int randomIndex = (int)(Math.random()*size);
        ServeProviderDTO providerDTO = dtoList.get(randomIndex);
        log.info("随机确定一个服务人员：{}" , providerDTO);
        return providerDTO;
    }
}
